#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

void swap(char* p1, char* p2,size_t width)
{
	for(int i = 0;i < width;i++)
	{
		char tmp = (*p1);
		(*p1) = (*p2);
		(*p2) = tmp;
		p1++;
		p2++;
	}
}
void bsort(void* base, size_t sz, size_t width, int(*cmp)(const void* p1, const void* p2))
{
	for (int i = 0;i < sz-1;i++)
	{
		for (int j = 0;j < sz - i - 1;j++)
		{
			if (cmp((char*)base + j * width , (char*)base + (j + 1) * width)>0)
			{
				swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
			}
		}
	}
}
void print_arr(int arr[], size_t sz)
{
	for (int i = 0;i < sz;i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}
void test1()
{
	int arr[10] = { 9,2,1,3,5,4,6,7,8,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bsort(arr, sz, sizeof(arr[0]), cmp);
	print_arr(arr, sz);
}
struct stu
{
	int age;
	char name[10];
};
int cmp_stu_by_age(const void* p1, const void* p2)
{
	return ((struct stu*)p1)->age - ((struct stu*)p2)->age;
}
void test2()
{
	struct stu arr[3] = { {15,"li"},{13,"zhang"},{41,"yang"} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bsort(arr, sz, sizeof(arr[0]), cmp_stu_by_age);
	for (int i = 0;i < sz;i++)
	{
		printf("%d %s", arr[i].age, arr[i].name);
		printf("\n");
	}
}

int main()
{
	test1();
	test2();
}